﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Single.Npoi
{
    public class NpoiMapNode<T>
    {
        public string Key { get; set; }

        public Expression<Func<T, object>> Expr { get; set; }

        public bool IsRequired { get; set; } = false;

        public int Width { get; set; } = 0;

        public Type Type
        {
            get { return Expr.Body.Type; }
        }

        public MemberExpression? MemberExpr
        {
            get
            {
                if (Expr.Body is UnaryExpression)
                {
                    return ((MemberExpression)((UnaryExpression)Expr.Body).Operand);
                }
                else if (Expr.Body is MemberExpression)
                {
                    return ((MemberExpression)Expr.Body);
                }
                return null;
            }
        }

        public PropertyInfo? Property
        {
            get
            {
                return MemberExpr?.Member as PropertyInfo;
            }
        }

        public NpoiMapNode(string key, Expression<Func<T, object>> expr, bool required = false, int width = 0)
        {
            Key = key; Expr = expr; IsRequired = required; Width = width;
        }
    }
}